<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2021/9/3
 * Time: 16:55
 */
namespace app\index\controller;


use app\BaseController;
use app\index\service\DyOpenService;
use app\index\service\Push;
use app\middleware\Check;
use Hedeqiang\TenIM\IM;
use JiaLeo\Payment\Applepay\IAP;
use JiaLeo\Payment\Common\PaymentException;
use think\facade\Db;
use think\facade\Log;
use think\facade\Queue;

class UserCard extends BaseController{
    protected $middleware = [
        Check::class => ['except' 	=> [] ]
    ];

    /**
     * 获取用户信息
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function info(){
        $user = Db::name('user_cards')
            ->where('user_id', uid())
            ->field(['id_card_font', 'id_card_back' , 'license'])
            ->find();
        if(empty($user)){
            $user = [
                'id_card_font' => '',
                'id_card_back' => '',
                'license' => ''
            ];
        }
        return success('用户身份信息', $user);
    }



    /**
     * 更新用户信息
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function save(){
        $param = input();
        $data = ['user_id' => uid()];
        if(isset($param['id_card_font']) && !empty($param['id_card_font'])){
            $data['id_card_font'] = $param['id_card_font'];
        }
        if(isset($param['id_card_back']) && !empty($param['id_card_back'])){
            $data['id_card_back'] = $param['id_card_back'];
        }
        if(isset($param['license']) && !empty($param['license'])){
            $data['license'] = $param['license'];
        }

        if(!empty($data)) {
            if (Db::name('user_cards')->where('user_id', uid())->count()) {
                $data['updated_at'] = date('Y-m-d H:i:s');
                Db::name('user_cards')->where('user_id', uid())->update($data);
            } else {
                $data['created_at'] = date('Y-m-d H:i:s');
                Db::name('user_cards')->insert($data);
            }
        }
        return success('更新用户信息成功');
    }
}